package com.leyou.item.controller;

import com.leyou.item.interfaces.pojo.Goods;
import com.leyou.item.interfaces.pojo.GoodsDetail;
import com.leyou.item.interfaces.pojo.Sku;
import com.leyou.item.interfaces.pojo.vo.GoodsPage;
import com.leyou.item.service.SpuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 新增商品的Controller
 * @version 1.0.0
 * @Author yong.Mr
 * @data 2019-8-9
 */
@Slf4j
@CrossOrigin
@RestController
public class SpuController {

    /**
     * 注入GoodsService
     */
    @Autowired
    private SpuService spuService;

    /**
     * 新增商品
     * @param goodsPage 接收前台传入的参数列表
     * @return ResponseEntity<Void>
     */
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody GoodsPage goodsPage) {
        spuService.saveGoods(goodsPage);
        spuService.saveSkuAndStock(goodsPage);
        
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 编辑商品
     * @param goodsPage
     * @return
     */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody GoodsPage goodsPage){
        this.spuService.updateGoods(goodsPage);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    /**
     * 根据spuId查询spuDetail
     * @params spuId 接收前台传入的参数列表
     * @return ResponseEntity<GoodsDetail>
     */
    @GetMapping("spu/detail/{spuId}")
    public ResponseEntity<GoodsDetail> querySpuDetailBySpuId(@PathVariable("spuId")Long spuId){
        GoodsDetail spuDetail = this.spuService.querySpuDetailBySpuId(spuId);
        if (spuDetail == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(spuDetail);
    }

    /**
     * 根据spuId查询sku的集合
     * @param spuId
     * @return
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<Sku>> querySkusBySpuId(@RequestParam("id")Long spuId){
        List<Sku> skus = this.spuService.querySkusBySpuId(spuId);
        if (CollectionUtils.isEmpty(skus)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(skus);
    }
    // 则是查询goods的商品id哦我去曹你妈的这是商品的id
   @GetMapping("{id}")
    public ResponseEntity<Goods> querySpuById(@PathVariable("id") Long id){
        Goods spu = this.spuService.querySpuById(id);
        if(spu == null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(spu);
    }

}

